# -*- coding: utf-8 -*-
"""
Created on Sun Feb  4 10:23:48 2018

@author: Luther
"""

print("Extract Version4.0_20180204")
print('By Luther')

import os
import re

#----------------定义检索函数------------------


def extract(text, regex):  #text为分割后的列表格式文本，rule为|连接的单条检索式
    results = []
    n_list = []
    for n in range(len(text)):
        match = regex.search(text[n])
        if match:
            n_list.append(n)
    for i in n_list:
        start = max(0, i - backward)
        end = min(i + 1 + forward, len(text) - 1)
        result = "//".join(text[start:end])
        result = result.replace(",", "")
        result = result.replace("\n", "/")
        results.append(result)
    return results


def extract2(text, regex):
    results = []
    n_list = []
    for n in range(len(text)):
        match = regex.search(text[n])
        if match:
            n_list.append(n)
    for i in n_list:
        result = text[i]
        results.append(result)
    return results


def not_extract(text, regex):
    results = []
    n_list = []
    for n in range(len(text)):
        match = regex.search(text[n])
        if not match:
            n_list.append(n)
    for i in n_list:
        result = text[i]
        results.append(result)
    return results


#--------------------END----------------------

#----------------配置基本信息------------------

go = ""
while go == "":
    workpath = os.path.dirname(__file__)
    path_input = os.path.join(workpath, "input")
    output_name = input('请定义该规则的名称：')
    output_file = output_name + ".csv"
    path_output = os.path.join(workpath, "output")
    os.chdir(path_output)
    with open(output_file, "w", encoding="utf-8") as f:
        f.write('code,id,content\n')

    punc = input('请输入要用于断句的标点，用“|”连接, 键入空值则默认为句号：')
    if punc == "":
        punc = "。"

    os.chdir(workpath)
    rule_path = input("请键检索规则文件名：")
    with open(rule_path) as f:
        rule_file = f.read()
    rule_list = rule_file.split("\n")

    out_rule = input('请输入要排除的关键词，键入空值则不排除任何词语：')

    backward = int(input("请输入向前步长："))
    forward = int(input("请输入向后步长："))

    #--------------------END----------------------

    #--------------读取TXT文件并检索---------------

    os.chdir(path_input)
    file_list = [
        x for x in os.listdir(path_input) if x.split(".")[-1] == "txt"
    ]
    count = 0
    for each in file_list:
        code = each[11:17]
        os.chdir(path_input)
        try:
            with open(each, encoding="gbk") as f:
                source = f.read()
        except:
            with open(each, encoding="utf-8", errors="ignore") as f:
                source = f.read()
        text = re.split(r'%s' % punc, source)
        for i in range(len(rule_list)):
            regex = re.compile(r'%s' % rule_list[i])
            if i == 0:
                text = extract(text, regex)
            else:
                text = extract2(text, regex)
        if out_rule != "":
            regex = re.compile(r'%s' % out_rule)
            text = not_extract(text, regex)

#--------------------END----------------------

#----------------保存检索结果------------------

        os.chdir(path_output)
        with open(output_file, "a", encoding="utf-8") as f:
            if len(text) == 0:
                f.write(code)
                f.write(',')
                f.write(str(0))
                f.write(',')
                f.write('无记录')
                f.write('\n')
            else:
                for j in range(len(text)):
                    f.write(code)
                    f.write(',')
                    f.write(str(j + 1))
                    f.write(',')
                    f.write(text[j])
                    f.write('\n')
        count = count + 1
        print("\r当前进度: {:.2f}%".format(count * 100 / len(file_list)), end="")

#--------------------END----------------------

    print("\nFinished!")
    go = input("回车以重新运行本程序，键入任何值退出：")
